FightManagerFunc={}
FightManager={}
FightManagerFunc.__index=FightManagerFunc
setmetatable(FightManager, FightManagerFunc)


local comp = function(a,b)
  return a.minjie>b.minjie
end

FightManagerFunc.getSortedHeros = function(self)
  local tmp = {}
  local i=1
  for k, v in pairs(self.attacks) do tmp[i] = v;tmp[i].isAttack=true;i=i+1;print(v.name);end
  for k, v in pairs(self.defends) do tmp[i] = v;tmp[i].isAttack=false;i=i+1;print(v.name);end 

  table.sort(tmp, comp)  
  for k,v in pairs(tmp) do
    print(k,v.name)
  end
  return tmp
end

FightManagerFunc.create = function(self, scene, attacks, defends)
  local fight={}
  fight.attacks = attacks
  fight.defends = defends  
  setmetatable(fight, FightManagerFunc)
  
  local tmp = {}
  local i=1
  for k, v in pairs(self.attacks) do tmp[i] = {k,v.minjie};tmp[i].isAttack=true;i=i+1;print(v.name);end
  for k, v in pairs(self.defends) do tmp[i] = {k,v.minjie};tmp[i].isAttack=false;i=i+1;print(v.name);end
  table.sort(tmp, comp)
  fight.herosSorted = tmp  
  
  tmp = {};i=1
  for k,v in pairs(self.attacks) do tmp[i]=k; i=i+1;end
  fight.attacksSeq = tmp

  tmp = {};i=1
  for k,v in pairs(self.defends) do tmp[i]=k; i=i+1;end
  fight.defendsSeq = tmp

  fight.attackIndex = 1

  FightView:create(scene, attacks, defends)
end

FightManagerFunc.getAttackTarget = function(self)
  local a = self.herosSorted[self.attackIndex]
  local target = {}
  local i=1
  if a.isAttack then
    local index = math.random(1,#self.defendsSeq)
    target[i] = index
  else
    local index = math.random(1,#self.attacksSeq)
    target[i] = index
  end
  return target
end

FightManagerFunc.play = function(self)
  local attackProcesses = {}
  while 1 do
    local attack = self.herosSorted[self.attackIndex]
    local targets = self.getAttackTarget()
    for k,v in pairs(targets) do 
      local hp = v.currentHP - attack.attack
      if hp < 0 then hp=0 end 
      v.currentHP = hp
      local tmpProcess = {attackerPos=}
    end
  end
end

FightManager:create({{name="h1", minjie=12},{name="h2", minjie=23}}, {{name="h3", minjie=12},{name="h4", minjie=33}})
